package com.itsu.vtalk.service.impl;

import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itsu.vtalk.dao.UserDao;
import com.itsu.vtalk.entity.User;
import com.itsu.vtalk.exception.AccountException;
import com.itsu.vtalk.exception.LoginAuthException;
import com.itsu.vtalk.exception.SystemBaseException;
import com.itsu.vtalk.service.LoginLogoutService;
import com.itsu.vtalk.vo.RestEntity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author suben
 * @createtime 2020/6/21 10:02
 **/
@Service
public class LoginLogoutServiceBase implements LoginLogoutService {

    @Resource
    private UserDao userDao;

    @Override
    public User checkLogin(User user) throws SystemBaseException {
        QueryWrapper<User> condition = new QueryWrapper<>();
        condition.eq("user_id", user.getUserId());
        condition.eq("password", DigestUtil.md5Hex(user.getPassword()));
        List<User> result = userDao.selectList(condition);
        if (result == null || result.isEmpty()) {
            throw new LoginAuthException(RestEntity.LOGIN_AUTH_ERROR.getReturnCode(), RestEntity.LOGIN_AUTH_ERROR.getReturnMsg());
        }
        if (result.size() >= 2) {
            throw new AccountException(RestEntity.ACCOUNT_ERROR.getReturnCode(), RestEntity.ACCOUNT_ERROR.getReturnMsg());
        }
        return result.get(0);
    }
}
